<?php
/**
 * @package LaravelBoot
 *
 * @internal
 *
 * @author mawenpei
 * @date 2019/3/16 21:14
 * @version
 */
namespace App\Console\Commands;

use App\MobileRecharge;
use App\Models\MobileRechargeTask;
use Carbon\Carbon;
use Illuminate\Console\Command;

class MobileRechargeCommand extends Command
{
    protected $signature = 'mobile:recharge';

    protected $description = 'Mobile Recharge';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $tasks = MobileRechargeTask::where('status', 0)
            ->where('pay_at', '<=', date('Y-m-d H:i:s'))
            ->lockForUpdate()->take(10)->get();
        $tasks = json_decode(json_encode($tasks),true);
        foreach ($tasks as $task) {
            $res = MobileRecharge::onlineOrder($task['mobile'],$task['amount'],$task['sn']);
            if($res===true){
                (new MobileRechargeTask())->query()->where('id',$task['id'])->update(['status'=>1]);
            }else{
                (new MobileRechargeTask())->query()->where('id',$task['id'])->update(['status'=>2]);
            }
        }
        $this->line('done');
    }

}